package com.fenxiangle.yueding.feature.focus.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fenxiangle.yueding.R;
import com.fenxiangle.yueding.common.widget.popupwindow.PopupWindowList;
import com.fenxiangle.yueding.entity.bo.IDCardBo;
import com.fenxiangle.yueding.entity.bo.IdentificationBo;
import com.fenxiangle.yueding.feature.identification.contract.IdentificationContract;
import com.fenxiangle.yueding.feature.identification.login.DaggerIdentificationComponent;
import com.fenxiangle.yueding.feature.identification.login.IdentificationPresenterModule;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.utils.T;

import java.io.File;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class IdentityAuthenticationActivity extends BaseActivity implements IdentificationContract.View {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.img_front)
    ImageView imgFront;
    @BindView(R.id.layout_front_ll)
    LinearLayout layoutFrontLl;
    @BindView(R.id.layout_front)
    RelativeLayout layoutFront;
    @BindView(R.id.img_inverse_side)
    ImageView imgInverseSide;
    @BindView(R.id.layout_inverse_side_ll)
    LinearLayout layoutInverseSideLl;
    @BindView(R.id.layout_inverse_side)
    RelativeLayout layoutInverseSide;


    @Inject
    IdentificationContract.Presenter presenter;
    @BindView(R.id.front_text)
    TextView frontText;
    private String idType="";
    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_identity_authentication;
    }

    @Override
    protected void initInjector() {
        DaggerIdentificationComponent.builder().identificationPresenterModule(new IdentificationPresenterModule(this)).build().inject(this);
    }

    String cardNegative, cardPositive,businessLicense;

    @Override
    protected void initData() {
        idType=getIntent().getStringExtra("idType");
        if(TextUtils.isEmpty(idType)){
            initToolBar(toolbar, "上传身份证", true, true);
        }else{
            initToolBar(toolbar, "上传营业执照", true, true);
            layoutInverseSide.setVisibility(View.GONE);
            frontText.setText("上传营业执照");
        }

        toolbar.inflateMenu(R.menu.menu_identity);
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                int menuItemId = item.getItemId();
                if (menuItemId == R.id.action_identity) {
                    if(TextUtils.isEmpty(idType)){
                        if(TextUtils.isEmpty(cardNegative)){
                            T.showShort("请选择身份证正面");
                            return true;
                        }
                        if(TextUtils.isEmpty(cardPositive)){
                            T.showShort("请选择身份证反边");
                            return true;
                        }
                        presenter.add_uploadIDCard_authentication(cardNegative, cardPositive);
                    }else{
                        if(TextUtils.isEmpty(businessLicense)){
                            T.showShort("请选择营业执照");
                            return true;
                        }
                        presenter.add_uploadBusinessLicense_authentication(businessLicense);
                    }
                }

                return false;
            }
        });
    }

    @OnClick({R.id.layout_front, R.id.layout_inverse_side})
    public void onViewClicked(View view) {
        PopupWindowList popupWindowList;
        switch (view.getId()) {
            case R.id.layout_front:
                popupWindowList = new PopupWindowList(this);
                popupWindowList.showMoreWindow(layoutFront, "拍照", "相册", new PopupWindowList.PopupWindowListener() {
                    @Override
                    public void first() {
                        PictureSelector.create(IdentityAuthenticationActivity.this)
                                .openCamera(PictureMimeType.ofImage())
                                .compress(true)
                                .forResult(10005);
                    }

                    @Override
                    public void second() {
                        // 进入相册 以下是例子：用不到的api可以不写
                        PictureSelector.create(IdentityAuthenticationActivity.this)
                                .openGallery(PictureMimeType.ofImage())//全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()、音频.ofAudio()
                                .theme(R.style.picture_default_style1)//主题样式(不设置为默认样式) 也可参考demo values/styles下 例如：R.style.picture.white.style
                                .maxSelectNum(1)// 最大图片选择数量 int
                                .minSelectNum(1)// 最小选择数量 int
                                .imageSpanCount(4)  // 每行显示个数 int
                                .isCamera(true)// 是否显示拍照按钮 true or false
                                .compress(true)// 是否压缩 true or false
                                .selectionMode(PictureConfig.MULTIPLE)// 多选 or 单选 PictureConfig.MULTIPLE or PictureConfig.SINGLE
                                .forResult(10005);//结果回调onActivityResult code     ;
                    }
                });
                break;
            case R.id.layout_inverse_side:
                popupWindowList = new PopupWindowList(IdentityAuthenticationActivity.this);
                popupWindowList.showMoreWindow(layoutInverseSide, "拍照", "相册", new PopupWindowList.PopupWindowListener() {
                    @Override
                    public void first() {
                        PictureSelector.create(IdentityAuthenticationActivity.this)
                                .openCamera(PictureMimeType.ofImage())
                                .compress(true)
                                .forResult(10006);
                    }

                    @Override
                    public void second() {
                        // 进入相册 以下是例子：用不到的api可以不写
                        PictureSelector.create(IdentityAuthenticationActivity.this)
                                .openGallery(PictureMimeType.ofImage())//全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()、音频.ofAudio()
                                .theme(R.style.picture_default_style1)//主题样式(不设置为默认样式) 也可参考demo values/styles下 例如：R.style.picture.white.style
                                .maxSelectNum(1)// 最大图片选择数量 int
                                .minSelectNum(1)// 最小选择数量 int
                                .imageSpanCount(4)  // 每行显示个数 int
                                .isCamera(true)// 是否显示拍照按钮 true or false
                                .compress(true)// 是否压缩 true or false
                                .selectionMode(PictureConfig.MULTIPLE)// 多选 or 单选 PictureConfig.MULTIPLE or PictureConfig.SINGLE
                                .forResult(10006);//结果回调onActivityResult code     ;
                    }
                });
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 10005:
                    // 图片、视频、音频选择结果回调
                    List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                    for (int i = 0; i < selectList.size(); i++) {
                        if (!TextUtils.isEmpty(selectList.get(i).getCompressPath())) {
                            AM.image().bind(new File(selectList.get(i).getCompressPath()), imgFront);
                            if(TextUtils.isEmpty(idType)){
                                cardNegative = selectList.get(i).getCompressPath();
                            }else {
                                businessLicense= selectList.get(i).getCompressPath();
                            }

                        } else {
                            T.showShort("图片错误");
                        }
                    }
                    layoutFrontLl.setVisibility(View.GONE);
                    // 例如 LocalMedia 里面返回三种path
                    // 1.media.getPath(); 为原图path
                    // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true  注意：音视频除外
                    // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true  注意：音视频除外
                    // 如果裁剪并压缩了，以取压缩路径为准，因为是先裁剪后压缩的
//                    adapter.setList(selectList);
//                    adapter.notifyDataSetChanged();
                    break;
                case 10006:
                    // 图片、视频、音频选择结果回调
                    selectList = PictureSelector.obtainMultipleResult(data);
                    for (int i = 0; i < selectList.size(); i++) {
                        if (!TextUtils.isEmpty(selectList.get(i).getCompressPath())) {
                            AM.image().bind(new File(selectList.get(i).getCompressPath()), imgInverseSide);
                            cardPositive = selectList.get(i).getCompressPath();
                        } else {
                            T.showShort("图片错误");
                        }
                    }
                    layoutInverseSideLl.setVisibility(View.GONE);
                    break;
            }
        }
    }

    @Override
    public void showLoginSuccess() {

    }

    @Override
    public void showData(IdentificationBo identificationBo) {

    }

    @Override
    public void showIdentificationData(IDCardBo idCardBo) {
        Intent intent = new Intent();
        if(TextUtils.isEmpty(idType)){
            intent.putExtra("cardNegative", idCardBo.getCardNegative());
            intent.putExtra("cardPositive", idCardBo.getCardPositive());
            setResult(30002, intent);
        }else{
            intent.putExtra("businessLicense", idCardBo.getBusinessLicense());
            setResult(30006, intent);
        }

        finish();
        T.showShort("上传成功");
    }

    @Override
    public void showError(String error) {
        T.showShort("错误");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
